﻿// OpenImageDlg.cpp: 实现文件
//

#include "pch.h"
#include "3180101584SX1.h"
#include "3180101584SX1Dlg.h"
#include "OpenImageDlg.h"
#include "afxdialogex.h"
#include "OpenImage.h"

extern OpenImageClass OImageC;
extern CMy3180101584SX1Dlg *MainDlg;

// OpenImageDlg 对话框

IMPLEMENT_DYNAMIC(OpenImageDlg, CDialog)

OpenImageDlg::OpenImageDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_DIALOG_OPENIMAGE, pParent)
{

}

OpenImageDlg::~OpenImageDlg()
{
}

void OpenImageDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(OpenImageDlg, CDialog)
	ON_BN_CLICKED(IDC_BUTTON1, &OpenImageDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON_IMAGE, &OpenImageDlg::OnBnClickedButtonImage)
	ON_BN_CLICKED(IDC_BUTTON_WINDOWS, &OpenImageDlg::OnBnClickedButtonWindows)
	ON_BN_CLICKED(IDC_BUTTON_OPENIMAGE, &OpenImageDlg::OnBnClickedButtonOpenimage)
	ON_BN_CLICKED(IDC_RADIO_GRAY, &OpenImageDlg::OnBnClickedRadioGray)
	ON_BN_CLICKED(IDC_RADIO_RGB, &OpenImageDlg::OnBnClickedRadioRgb)
	ON_BN_CLICKED(IDC_RADIO_ZJLD, &OpenImageDlg::OnBnClickedRadioZjld)
	ON_BN_CLICKED(IDC_RADIO_SXX, &OpenImageDlg::OnBnClickedRadioSxx)
END_MESSAGE_MAP()


// OpenImageDlg 消息处理程序


void OpenImageDlg::OnBnClickedButton1()
{
	CString FileName;

	LPCTSTR szFilter = _T("000 files(*.00)|*.000|");		//提示保存文件的类型
	CFileDialog dlg(FALSE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilter);		// CFileDialog的第一个参数标明是浏览(打开，TRUE)还是保存（FALSE）

	if (dlg.DoModal() == IDOK)		//返回值是OK，说明点击了“保存”/“确认”按钮，返回值是IDCANCEL，说明点击了取消
	{
		FileName = dlg.GetPathName();		//保存文件的路径到filename变量中
	}

	if (FileName == "")
	{
		MessageBox("保存文件路径为空！");
		return;
	}

	OImageC.etmImg = (ETMIMG *)malloc(sizeof(ETMIMG));

	OImageC.ReadEtm000File_Func(FileName);

	OImageC.etmImg->EdRmin = OImageC.Rmin; 
	OImageC.etmImg->EdRmax = OImageC.Rmax;
	OImageC.etmImg->EdGmin = OImageC.Gmin; 
	OImageC.etmImg->EdGmax = OImageC.Gmax;
	OImageC.etmImg->EdBmin = OImageC.Bmin; 
	OImageC.etmImg->EdBmax = OImageC.Bmax;

	const char * B000name = NULL;
	int Widt = OImageC.pImgW;
	int Heig = OImageC.pImgH;

	OImageC.Gene_Char_Etmimg_MemorySpace(Heig, Widt, OImageC.etmImg);

	OImageC.ReadEtmFileData_Func(OImageC.etmImg, B000name);

	CEdit* pBoxOne;
	CString Item;

	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_BACKGROUND);
	Item.Format("%d", 0);

	pBoxOne->SetWindowText(Item);

	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_STARTX);
	Item.Format("%d", 0);

	pBoxOne->SetWindowText(Item);

	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_STARTY);
	Item.Format("%d", 0);

	pBoxOne->SetWindowText(Item);

	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_WIDTH);
	Item.Format("%d", OImageC.pImgW);

	pBoxOne->SetWindowText(Item);

	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_HEIGHT);
	Item.Format("%d", OImageC.pImgH);

	pBoxOne->SetWindowText(Item);

	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_R);

	pBoxOne->SetWindowText(OImageC.Rname);

	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_G);

	pBoxOne->SetWindowText(OImageC.Gname);

	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_B);

	pBoxOne->SetWindowText(OImageC.Bname);

	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_M);

	pBoxOne->SetWindowText(OImageC.Mname);

	AfxMessageBox("打开图像完成");
	// TODO: 在此添加控件通知处理程序代码
}


void OpenImageDlg::OnBnClickedButtonImage()
{
	CEdit* pBoxOne;
	CString Item;

	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_STARTWIDTH);
	Item.Format("%d", OImageC.pImgW);

	pBoxOne->SetWindowText(Item);

	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_STARTHEIGHT);
	Item.Format("%d", OImageC.pImgH);

	pBoxOne->SetWindowText(Item);
	// TODO: 在此添加控件通知处理程序代码
}


void OpenImageDlg::OnBnClickedButtonWindows()
{
	CEdit* pBoxOne;
	CString Item;

	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_STARTWIDTH);
	Item.Format("%d", 1824);

	pBoxOne->SetWindowText(Item);

	pBoxOne = (CEdit*)GetDlgItem(IDC_EDIT_STARTHEIGHT);
	Item.Format("%d", 950);

	pBoxOne->SetWindowText(Item);
}


void OpenImageDlg::OnBnClickedButtonOpenimage()
{
	UpdateData(TRUE);

	OImageC.SetGrayBol = SetGrayBol;

	CString soffx;
	GetDlgItemText(IDC_EDIT_STARTX, soffx);

	CString soffy;
	GetDlgItemTextA(IDC_EDIT_STARTY,soffy);

	OImageC.etmImg->ImgXoff = _ttoi(soffx);

	OImageC.etmImg->ImgYoff = _ttoi(soffy);

	

	CString theImgH;
	GetDlgItemTextA(IDC_EDIT_STARTHEIGHT, theImgH);

	CString theImgW;
	GetDlgItemText(IDC_EDIT_STARTWIDTH, theImgW);
	int iImgW = _ttoi(theImgW);

	int iImgH = _ttoi(theImgH);

	OImageC.etmImg->FastData = NULL;
	OImageC.etmImg->FastWidth = OImageC.etmImg->FastHeight = 0;

	OImageC.ChaZhiBool = ChaZhiBol;

	OImageC.GencRemoteImgFunc(OImageC.etmImg, iImgW, iImgH);

	MainDlg->Invalidate();
	MainDlg->SendMessageA(WM_PAINT);

	OImageC.PaintRemoteImgFunc(hdc, OImageC.etmImg);

	OImageC.Get_Data_UChar_Fact_Func(OImageC.etmImg->Img_H0, OImageC.etmImg->Img_W0, 0, OImageC.etmImg->Rimg, OImageC.etmImg->factR);
	//OImageC.etmImg->EdRmin = OImageC.fmin; OImageC.etmImg->EdRmax = OImageC.fmax;

	OImageC.Get_Data_UChar_Fact_Func(OImageC.etmImg->Img_H0, OImageC.etmImg->Img_W0, 0, OImageC.etmImg->Gimg, OImageC.etmImg->factG);
	//OImageC.etmImg->EdGmin = OImageC.fmin; OImageC.etmImg->EdGmax = OImageC.fmax;

	OImageC.Get_Data_UChar_Fact_Func(OImageC.etmImg->Img_H0, OImageC.etmImg->Img_W0, 0, OImageC.etmImg->Bimg, OImageC.etmImg->factB);
	//OImageC.etmImg->EdBmin = OImageC.fmin; OImageC.etmImg->EdBmax = OImageC.fmax;

	OImageC.etmImg->Rmax = OImageC.etmImg->Gmax = OImageC.etmImg->Bmax = 0;

	for (int i = 0; i < 255; i++)
	{
		if (OImageC.etmImg->Rmax <= OImageC.etmImg->factR[i])
			OImageC.etmImg->Rmax = OImageC.etmImg->factR[i];
		if (OImageC.etmImg->Gmax <= OImageC.etmImg->factG[i])
			OImageC.etmImg->Gmax = OImageC.etmImg->factG[i];
		if (OImageC.etmImg->Bmax <= OImageC.etmImg->factB[i])
			OImageC.etmImg->Bmax = OImageC.etmImg->factB[i];
	}
}


BOOL OpenImageDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	CWnd* pWnd = AfxGetMainWnd();//获取主窗口指针
	CDC * pDC = pWnd->GetDC();//获取主对话框CDC指针
	hdc = pDC->GetSafeHdc();

	((CButton *)GetDlgItem(IDC_RADIO_GRAY))->SetCheck(FALSE); //选上
	((CButton *)GetDlgItem(IDC_RADIO_RGB))->SetCheck(TRUE);//不选上

	((CButton *)GetDlgItem(IDC_RADIO_ZJLD))->SetCheck(FALSE); //选上
	((CButton *)GetDlgItem(IDC_RADIO_SXX))->SetCheck(TRUE);//不选上

	SetGrayBol = FALSE;
	ChaZhiBol = TRUE;
	
	GetWindowRect(&rect);//得到所处窗口的绝对坐标;    
	GetClientRect(&rect);//得到相对坐标

	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

void OpenImageDlg::set_parent_dlg(CDialog * parnet_dlg)
{
	m_pMianDlg = parnet_dlg;
}


void OpenImageDlg::OnBnClickedRadioGray()
{
	SetGrayBol = TRUE;
	// TODO: 在此添加控件通知处理程序代码
}


void OpenImageDlg::OnBnClickedRadioRgb()
{
	SetGrayBol = FALSE;
	// TODO: 在此添加控件通知处理程序代码
}


void OpenImageDlg::OnBnClickedRadioZjld()
{
	ChaZhiBol = FALSE;
}


void OpenImageDlg::OnBnClickedRadioSxx()
{
	ChaZhiBol = TRUE;
}
